home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / MEMMON_F / MUTILS.C < prev    next >
Text File  |  1990-03-02  |  5KB  |  169 lines

  1. /*
  2.  * mutils.c: utility functions used by memmon but not peculiar to it.
  3.  */
  4.  
  5. #include <ctype.h>
  6. #include "memmon.h"
  7.  
  8. /*
  9.  * chop(s, w, n, c) - break string into fields, a la awk(1).
  10.  *
  11.  *  s is modified in place with '\0' replacing occurrences of separator char c.
  12.  *  If c is zero, any *string of* whitespace is a single separator.
  13.  *
  14.  *  w is an array of n char pointers to receive addresses of the fields.
  15.  *  The return value gives the number of fields actually found;
  16.  *  additional entries in w are given the address of a null string.
  17.  */
  18. int chop(s, w, n, c)
  19. char *s, *w[];
  20. int n, c;
  21.    {
  22.    int i;
  23.  
  24.    if (!c)            /* skip leading whitespace in whitespace mode */
  25.       while (isspace(*s))
  26.          s++;
  27.    for (i = 0; i < n && *s;) {  /* while array not full and string not empty */
  28.       w[i++] = s;            /* store field address */
  29.       if (c)
  30.          while (*s && *s != c)        /* skip to particular separator */
  31.             s++;
  32.       else
  33.          while (*s && !isspace(*s))    /* skip to whitespace */
  34.             s++;
  35.       if (!*s)                /* break at end of string */
  36.          break;
  37.       *s++ = '\0';            /* terminate field and advance */
  38.       if (!c)
  39.          while (isspace(*s))        /* skip multi spaces in c=0 mode */
  40.             s++;
  41.    }
  42.    while (i < n)        /* fill rest of array with pointer to "" */
  43.       w[--n] = s;
  44.    return i;            /* return count of fields found */
  45.    }
  46.  
  47. /*
  48.  * trim(s, c) -- trim string at comments character c.
  49.  *
  50.  *  The string s is trimmed in place by storing a null character after the last
  51.  *  "significant" character.  A pointer to the first "significant" character is
  52.  *  returned.
  53.  *
  54.  *  If c is nonzero, it indicates a comment character;  the first appearance
  55.  *  of c that is not escaped by '\' indicates the start of the comments field.
  56.  *  Then (in any event) trailing whitespace is skipped.
  57.  */
  58. char *trim(s, c)
  59. char *s;
  60. int c;
  61.    {
  62.    char *p;
  63.    char *index();
  64.  
  65.    while (isspace(*s))            /* skip initial whitespace */
  66.       s++;
  67.    p = 0;
  68.    if (c)
  69.       for (p = s; p = index(p, c); p++)
  70.          if (p == s || p[-1] != '\\')    /* look for unescaped comment */
  71.             break;
  72.    if (!p)
  73.       p = s + strlen(s);        /* or find end if none */
  74.    while (p > s && isspace(p[-1]))    /* find last nonwhite */
  75.       --p;
  76.    *p = '\0';                /* terminate after last nonwhite */
  77.    return s;                /* and return pointer */
  78.    }
  79.  
  80. /*
  81.  * pstext(s) - write s as a PostScript string.
  82.  */
  83. novalue pstext(s)
  84. char *s;
  85.    {
  86.    char c;
  87.  
  88.    putchar('(');
  89.    while (c = *s++)  {
  90.       if (c == '\\' || c == '(' || c == ')')
  91.          putchar('\\');            /* protect \\ \( \) */
  92.       putchar(c);            /* output char */
  93.       }
  94.    putchar(')');
  95.    }
  96.  
  97. /*
  98.  * pexit(file) - issue perror() message and abort.
  99.  */
  100. novalue pexit(file)
  101. char *file;
  102.    {
  103.    fprintf(stderr, "%s: ", progname);
  104.    perror(file);
  105.    exit(ErrorExit);
  106.    }
  107.  
  108. /*
  109.  * litout() - set literal output mode on stdout, iff it's a tty.
  110.  * also set 9600 baud if current speed is unreasonable.
  111.  *
  112.  * Litout is only needed by binary output formats (mmrt/mmaed, not mmps or
  113.  * mmmeta).  THE CODE IS BSD SYSTEM DEPENDENT; a null routine is included
  114.  * for other systems, and a manual stty command may be needed there.
  115.  */
  116.  
  117. #ifndef GenericBSD
  118. novalue litout ()    /* not BSD -- will need to set tty modes manually */
  119.    {
  120.    }
  121. #else                    /* GenericBSD */
  122.  
  123. /*
  124.  * The following kludge allows compilation under Vax 4.3BSD by an ANSI C
  125.  *  compiler such as gcc.  It fixes a problem in <sgtty.c>.  The fix is not
  126.  *  general, but it works for tty devices, and that is sufficient here.
  127.  */
  128.  
  129. #ifdef Standard
  130. #ifdef vax
  131. #define IOCPARM_MASK    0x7f
  132. #define IOC_VOID    0x20000000
  133. #define IOC_OUT        0x40000000
  134. #define IOC_IN        0x80000000
  135. #define _IO(t,y)    (IOC_VOID|('t'<<8)|y)
  136. #define _IOR(t,y,z)    (IOC_OUT|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
  137. #define _IOW(t,y,z)    (IOC_IN|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
  138. #define _IOWR(t,y,z)    (IOC_INOUT|((sizeof(z)&IOCPARM_MASK)<<16)|('t'<<8)|y)
  139. #endif                    /* vax */
  140. #endif                    /* Standard */
  141.  
  142. #include <sgtty.h>
  143.  
  144. novalue litout()
  145.    {
  146.    struct sgttyb ttyb;            /* for setting tty attributes */
  147.    static int ldisc = NTTYDISC;
  148.    static int lbits = LLITOUT;
  149.    int fd;
  150.  
  151.    fd = fileno(stdout);
  152.    if (!isatty(fd))
  153.       return;
  154.    if (ioctl(fd, TIOCSETD, (char *) &ldisc))
  155.       pexit("can't select new tty driver");
  156.    if (ioctl(fd, TIOCGETP, (char *) &ttyb))
  157.       pexit("can't get sgtty block");
  158.    if (ioctl(fd, TIOCLBIS, (char *) &lbits))
  159.       pexit("can't set LLITOUT");
  160.    ttyb.sg_flags &= ~(RAW + ECHO);
  161.    ttyb.sg_flags |= CRMOD;
  162.    if (ttyb.sg_ospeed < B1200)            /* if speed obviously bogus, */
  163.       ttyb.sg_ispeed = ttyb.sg_ospeed = B9600;    /* try 9600 as a better guess */
  164.    if (ioctl(fd, TIOCSETN, (char *) &ttyb))
  165.       pexit("can't set tty attributes");
  166.    }
  167.  
  168. #endif                    /* GenericBSD */
  169.